#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 505;

ll k, e, n;
struct Node {
  ll x, f, c;
  bool operator<(Node a) const { return x < a.x; }
} a[N];
ll tot;
ll f[N][10005];
ll g(int i, int j) {
  return f[i][j] + (a[i + 1].x - a[i].x) * j * j - j * a[i + 1].c;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> k >> e >> n;
  rep(i, 1, n) cin >> a[i].x >> a[i].f >> a[i].c;
  sort(a + 1, a + n + 1);
  rep(i, 1, a[1].f) f[1][i] = i * a[1].c;
  tot = a[1].f;
  rep(i, 2, n) {
    deque<int> dq;
    int pos = 0;
    rep(j, 1, min(k, tot + a[i].f)) {
      ll mn = j - a[i].f;
      while (pos <= min((ll)j, tot)) {
        while (dq.size() && g(i - 1, dq.back()) >= g(i - 1, pos)) dq.pop_back();
        dq.push_back(pos++);
      }
      while (dq.size() && dq.front() < mn) dq.pop_front();
      f[i][j] = j * a[i].c + g(i - 1, dq.front());
    }
    tot += a[i].f;
  }
  cout << f[n][k] + 1ll * k * k * (e - a[n].x);
  return 0;
}